import { createRouter, createWebHashHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
const routes = [{
        path: '/',
        redirect: '/home'
    },
    {
        path: '/Login',
        name: 'Login',
        component: () =>
            import ('../views/Login.vue')
    },
    {
        path: '/Home',
        name: 'Home',
        component: () =>
            import ('../views/Home.vue'),
        // 子路由path不要加/
        children: [{
                path: '',
                component: () =>
                    import ('../components/Velcome.vue')
            },
            {
                path: 'categories',
                component: () =>
                    import ('../components/Categories.vue')
            },
            {
                path: 'goods',
                component: () =>
                    import ('../components/Goods.vue')
            },
            {
                path: 'orders',
                component: () =>
                    import ('../components/Orders.vue')
            },
            {
                path: 'params',
                component: () =>
                    import ('../components/Params.vue')
            },
            {
                path: 'reports',
                component: () =>
                    import ('../components/Reports.vue')
            },
            {
                path: 'rights',
                component: () =>
                    import ('../components/Rights.vue')
            },
            {
                path: 'roles',
                component: () =>
                    import ('../components/Roles.vue')
            },
            {
                path: 'users',
                component: () =>
                    import ('../components/Users.vue')
            }
        ]
    }

]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})

// 配置路由前置导航守卫,to.path从哪来,from.path到哪去,next放行
router.beforeEach((to, from, next) => {
    NProgress.start()
    if (to.path == '/login' || window.sessionStorage.token) return next()
    next('/login')
})
router.afterEach((to, from, next) => {
    NProgress.done()
})


export default router